package com.base.cn.platform.os.controller.op.feedback;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.op.feedback.OpFeedback;
import com.base.cn.platform.os.service.op.feedback.OpFeedbackBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 用户反馈Controller
 * @author zsw
 * @create 2018/4/26
 */
@RestController
@RequestMapping("/manage/op")
public class OpFeedbackController extends BaseController {

    @Autowired
    private OpFeedbackBiz opFeedbackBiz;


    /**
     * 查询反馈数量
     * @param feedback 反馈实体
     * @return
     */
    @RequestMapping("/feedbackCount")
    public BigDecimal count(@RequestBody OpFeedback feedback){
        return opFeedbackBiz.count(feedback);
    }

    /**
     * 查询用户反馈列表
     * @param currentPage 当前页数
     * @param feedback 反馈实体
     * @return PageInfo<OpFeedback>
     */
    @RequestMapping("/queryFeedbackList")
    public PageInfo<OpFeedback> queryFeedbackList(@RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage,
                                                  @RequestBody OpFeedback feedback,
                                                  @RequestParam(value = "sysUserData",required = false,defaultValue = "false")boolean sysUserData) {
        Pagination feedbackPage = new Pagination();
        feedbackPage.setCurrentPage(currentPage);
        PageInfo<OpFeedback> feedbackPageInfo = opFeedbackBiz.queryFeedbackList(feedbackPage, feedback);
        return feedbackPageInfo;
    }

    /**
     * 保存反馈
     * @param feedback 反馈实体
     * @return 结果数据
     */
    @RequestMapping("/saveUpdateFeedback")
    public Map<String, Object> saveUpdateFeedback(@RequestBody OpFeedback feedback){
        if (StringUtils.isNotEmpty(feedback)){
            if (DataUtil.idIsNotNull(feedback.getId())){
                if (StringUtils.isEmpty(feedback.getReplyContext())){
                    return ResultUtil.ERROR("请填写回复内容");
                }
            }else {
                if (StringUtils.isEmpty(feedback.getContext())){
                    return ResultUtil.ERROR("请填写反馈内容");
                }
            }
        }else{
            return ResultUtil.SUCCESS("反馈内容不能为空");
        }

        if (DataUtil.idIsNotNull(feedback.getId())){
            opFeedbackBiz.saveUpdateFeedback(feedback);
            return ResultUtil.SUCCESS("反馈回复成功");
        }else {
            feedback.setStatus(1);
            opFeedbackBiz.save(feedback);
            return ResultUtil.SUCCESS("反馈提交成功");
        }
    }

    /**
     * 删除用户反馈
     * @param feedbackIds
     * @return 结果数据
     */
    @RequestMapping("/deleteFeedback")
    public Map<String, Object> deleteFeedback(@RequestParam("feedbackIds")String feedbackIds){
        if (ObjectUtils.isNotEmpty(feedbackIds)){
            opFeedbackBiz.deleteFeedback(feedbackIds);
            return ResultUtil.SUCCESS("删除成功");
        }
        return ResultUtil.ERROR("删除失败");
    }

    /**
     * 根据id查询反馈
     *
     * @param feedbackId 反馈id
     * @return
     */
    @RequestMapping("/findFeedbackById")
    public OpFeedback findFeedbackById(@RequestParam("feedbackId") BigDecimal feedbackId){
        return opFeedbackBiz.findFeedbackById(feedbackId);
    }

}
